import axios from 'axios'
import { Toast } from 'vant'
import $throttle from '../../public/js/speed'
import router from '../router'

const ins = axios.create({
    baseURL: 'http://192.168.124.6:4000/api'
    // baseURL: 'http://api.chengqige.com:4000/api'
})

// 自定义拦截器
// 请求拦截器
ins.interceptors.request.use((config) => {
    config.headers.token = localStorage.getItem('token') || ''
    if (config.headers.loading) {
        $throttle(() => {
            Toast.loading({
                duration: 0,
                forbidClick: false,
                message: '加载中'
            })
        }, 100)
    }
    return config
}, error => Promise.reject(error))

// 响应拦截器
ins.interceptors.response.use((response) => {
    //10119是后端未登录的
    if (response.data.code === 10119) {
        router.push('/login')
    }
    setTimeout(() => {
        Toast.clear()
    }, 500)
    return response
}, error => Promise.reject(error))

export default ins
